<template>
  <el-form ref="formRef" :model="formData" :rules="rules" label-position="top">
    <el-form-item :label="$t('login.email')" prop="email">
      <el-input v-model="formData.email" :placeholder="$t('login.emailPlaceholder')">
        <template #prefix>
          <el-icon><Message /></el-icon>
        </template>
      </el-input>
    </el-form-item>

    <el-form-item :label="$t('login.password')" prop="password">
      <el-input
        v-model="formData.password"
        type="password"
        :placeholder="$t('login.passwordPlaceholder')"
        show-password
      >
        <template #prefix>
          <el-icon><Lock /></el-icon>
        </template>
      </el-input>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" :loading="loading" class="submit-btn" @click="handleSubmit">
        {{ $t('login.login') }}
      </el-button>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()

const emit = defineEmits(['login-success'])
const formRef = ref()
const loading = ref(false)

const formData = reactive({
  email: '',
  password: '',
})

const rules = {
  email: [
    { required: true, message: 'Please input email', trigger: 'blur' },
    { type: 'email', message: 'Please input correct email', trigger: 'blur' },
  ],
  password: [
    { required: true, message: 'Please input password', trigger: 'blur' },
    { min: 6, message: 'Password length should be at least 6 characters', trigger: 'blur' },
  ],
}

const handleSubmit = async () => {
  router.push('/home')
  // if (!formRef.value) return

  // try {
  //   await formRef.value.validate()
  //   loading.value = true

  //   // TODO: 调用登录 API
  //   // const res = await login(formData)

  //   ElMessage.success('Login successful')
  //   emit('login-success')
  // } catch (error) {
  //   console.error(error)
  //   ElMessage.error('Login failed')
  // } finally {
  //   loading.value = false
  // }
}
</script>

<style scoped>
.submit-btn {
  width: 100%;
}
</style>
